package com.framework.dubbo.consumer.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import com.common.utils.constant.Constants;
import com.framework.dubbo.FileSystemConfig;
import com.framework.dubbo.Pdf2HtmlService;
import com.framework.minio.config.MinioConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.InputStream;

/**
 * 描述：
 * dubbo服务的映射接口
 *
 * @author xianggj
 * @Date 2021/8/31 10:50
 **/
@Slf4j
public class DubboService {

    /**
     * pdf服务
     */
    @DubboReference(version = "2.0.0")
    private Pdf2HtmlService pdf2HtmlService;

    @Autowired
    private MinioConfig minioConfig;


    /**
     * 文件转html
     * @param stream 文件流
     * @param fileName 文件名称 ！必须包含后缀！
     * @return
     */
    public String file2html(InputStream stream, String fileName) throws Exception{
        TimeInterval timer = DateUtil.timer();
        byte[] bytes = IoUtil.readBytes(stream);
        String context = pdf2HtmlService.pdf2html(bytes, fileName, false);
        log.info("转换文件{}耗时：{}ms", fileName, timer.interval());
        return context;
    }

    /**
     * 文件转html
     *  需要服务端和客户端配置相同的文件中心
     * @param filePath 文件系统中文件路径
     * @return
     */
    public String file2html(String filePath, String bid) throws Exception{
        TimeInterval timer = DateUtil.timer();
        FileSystemConfig fileSystemConfig = getFileSystemConfig();
        String newPath = pdf2HtmlService.pdf2html(fileSystemConfig, filePath, bid, false);
        log.info("转换文件{}耗时：{}ms", filePath, timer.interval());
        return newPath;
    }
    /**
     * 文件转字符串
     *  需要服务端和客户端配置相同的文件中心
     * @param filePath 文件系统中文件路径
     * @return
     */
    public String file2Str(String filePath) throws Exception{
        TimeInterval timer = DateUtil.timer();
        FileSystemConfig fileSystemConfig = getFileSystemConfig();
        filePath = StrUtil.removeAll(filePath, Constants.DOUBLE_QUOTATION);
        filePath = StrUtil.removeAll(filePath, Constants.SINGAL_QUOTATION);
        String newPath = pdf2HtmlService.pdf2Str(fileSystemConfig, filePath, true);
        log.info("转换文件{}耗时：{}ms", filePath, timer.interval());
        return newPath;
    }

    private FileSystemConfig getFileSystemConfig() {
        FileSystemConfig fileSystemConfig = new FileSystemConfig();
        fileSystemConfig.setAccessKey(minioConfig.getAccessKey());
        fileSystemConfig.setBucketName(minioConfig.getBucketName());
        fileSystemConfig.setDownloadPrefix(minioConfig.getDownloadPrefix());
        fileSystemConfig.setEndpoint(minioConfig.getEndpoint());
        fileSystemConfig.setRunningDirs(minioConfig.getRunningDirs());
        fileSystemConfig.setSecretKey(minioConfig.getSecretKey());
        fileSystemConfig.setSourceDirs(minioConfig.getSourceDirs());
        fileSystemConfig.setTargetDirs(minioConfig.getTargetDirs());
        return fileSystemConfig;
    }


}
